/**
 * 
 */
package org.opencare.ndwc.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * @author Nont Banditwong
 * 
 */
@Entity
@Table(name = "t_event")
public class Event implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5229428888307444044L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@Column(name = "identifier")
	private String identifier;
	
	@Column(name = "topic", nullable = false)
	private String topic;

	@Lob
	@Column(name = "description")
	private String description;
	
	@Column(name = "mapPath")
	private String mapPath;
		
	@ManyToOne(optional = false, fetch = FetchType.LAZY)
	@JoinColumn(name = "has_event_type_id")
	private EventType hasEventType;

	@ManyToOne(optional = false, fetch = FetchType.LAZY)
	@JoinColumn(name = "has_level_id")
	private EventLevel hasLevel;

	@ManyToOne(optional = false, fetch = FetchType.LAZY)
	@JoinColumn(name = "has_disaster_type_id")
	private DisasterType hasDisasterType;

	@ManyToOne
	@JoinColumn(name = "event_parent")
	private Event parent;

	@OneToMany(mappedBy = "parent", cascade = { CascadeType.MERGE,
			CascadeType.PERSIST })
	private List<Event> children = new ArrayList<Event>();

	@Temporal(value = TemporalType.TIMESTAMP)
	@Column(name = "date_created", nullable = false)
	private Date dateCreated;
	
	
	/**
	 * 
	 */
	public Event() {
		// TODO Auto-generated constructor stub
		Date now = new Date();
		this.dateCreated = now;		
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public String getMapPath() {
		return mapPath;
	}

	public void setMapPath(String mapPath) {
		this.mapPath = mapPath;
	}

	public EventType getHasEventType() {
		return hasEventType;
	}

	public void setHasEventType(EventType hasEventType) {
		this.hasEventType = hasEventType;
	}

	public EventLevel getHasLevel() {
		return hasLevel;
	}

	public void setHasLevel(EventLevel hasLevel) {
		this.hasLevel = hasLevel;
	}

	public DisasterType getHasDisasterType() {
		return hasDisasterType;
	}

	public void setHasDisasterType(DisasterType hasDisasterType) {
		this.hasDisasterType = hasDisasterType;
	}

	public Event getParent() {
		return parent;
	}

	public void setParent(Event parent) {
		this.parent = parent;
	}

	public List<Event> getChildren() {
		return Collections.unmodifiableList(children);
	}

	public void addChildEvent(Event event) {
		if (event == null) {
			throw new IllegalArgumentException("child event is null!");
		}

		// check to see if comment has a parent
		if (event.getParent() != null) {
			// if there is a parent check to see if it's already
			// associated to this comment
			if (event.getParent().equals(this)) {
				// if this instance is already the parent, we can just return
				return;
			} else {
				// disconnect post from it's current relationship
				event.getParent().children.remove(this);
			}
		}

		// make this instance the new parent
		event.setParent(this);
		children.add(event);
	}

	public void removeChildEvent(Event event) {
		if (event == null) {
			throw new IllegalArgumentException("child comment is null!");
		}

		// make sure we are the parent before we break the relationship
		if (event.parent != null && event.getParent().equals(this)) {
			event.setParent(null);
			children.remove(event);
		} else {
			throw new IllegalArgumentException(
					"child event not associated with this instance");
		}
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

}
